package cn.com.controller.wxController;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.com.Enum.OrderStatus;
import cn.com.constant.Code;
import cn.com.db.model.DiyOrder;
import cn.com.db.model.WxDiyPayResult;
import cn.com.db.service.DiyOrderService;
import cn.com.db.service.WxDiyPayResultService;
import cn.com.db.service.WxPayResultService;
import cn.com.util.DataResult;
import cn.com.util.JsonUtil;
import cn.com.util.TimeUtil;
import cn.com.util.UidUtil;
import cn.com.wxUtil.ReturnSignUtil;

@RestController
@RequestMapping("/wx/")
public class ReturnDiyOrderSignController {
	@Autowired
	DiyOrderService diyOrderService;
	@Autowired
	WxPayResultService wxPayResultService;
	@Autowired
	WxDiyPayResultService wxDiyPayResultService;
	@RequestMapping("doDiyOrder")
	public String doOrder(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("diyOrderId") String orderIdList, @RequestParam("code") String code // 临时登陆凭证,前端请求微信服务器获取
	) throws Exception {
		DataResult dataResult = new DataResult();
		
		// 分割订单 同一家商店配送费价格不能重复计算
		BigDecimal price = new BigDecimal("0.00");
		List<String> idList = UidUtil.splitStringByLine(orderIdList);
		Set<String> stores = new HashSet<>();
		List<DiyOrder> orderList = diyOrderService.getDiyOrders(idList);
		StringBuffer bodyBuffer = new StringBuffer("");
		for (DiyOrder order : orderList) {
			bodyBuffer.append(order.getDiyWork().getName()).append(",");
			price = order.getPrice().add(price);
			String store = order.getStore();
			if (!stores.contains(store)) {
				stores.add(store);
				if (order.getFreight() != null) {
					price = order.getFreight().add(price);
				}
			}
		}
		String body = bodyBuffer.substring(0,bodyBuffer.length()-1);
		String priceString = String.valueOf(price.movePointRight(2).intValue());
		String xprId = UidUtil.createUUID();
		String backUrl = "http://118.24.106.223:8080/wx/wxPayNotify/diyOrder";
		String prepayId = "";
		String result = "";
		try {
			prepayId = ReturnSignUtil.getPrepayId(request, orderIdList, code, backUrl, body, priceString,xprId);
			result = ReturnSignUtil.getFiveParam(prepayId);
		} catch (Exception e) {
			String message = e.getMessage();
			dataResult.setData(message);
			dataResult.setCode(Code.ERRORPARM);
			dataResult.setMessage("获取失败");
			return JsonUtil.toJsonString(dataResult);
		}
		WxDiyPayResult wxDiyPayResult = new WxDiyPayResult();

		wxDiyPayResult.setId(xprId);
		wxDiyPayResult.setOrder_id(orderIdList);
		wxDiyPayResult.setCreated_at(TimeUtil.getNowTimestamp());
		wxDiyPayResult.setUpdated_at(TimeUtil.getNowTimestamp());
		wxDiyPayResult.setResult(OrderStatus.WAIT_PAY.getStatus());
		wxDiyPayResult.setPrepay_id(prepayId);
		wxDiyPayResultService.createWxDiyPayResult(wxDiyPayResult);
		return result;
	}
}
